#!/usr/bin/env bash
# 实例文件：cdscript
# cdscript - 准备并刻录目录中的文件
# 
# 用法：cdscript dir [ cddev ]
#
if (( $#<1 || $# > 2 ))
then 
    echo 'usage: cdscript dir [ cddev ]'
    exit 2
fi

SRCDIR=$1
CDDEV=${2:- "ATAPI:0,0,0"}
ISOIMAGE=/tmp/cd$$.iso

echo "building ISO image..."
mkisofs -A "$(cat ~/.cdAnnotation)" \
  -p "$(hostname)" -V "${SRCDIR##*/}" \
  -r -o "$ISOIMAGE" $SRCDIR
STATUS=$?
if (( STATUS != 0 ))
then
    echo "Error. ISO image failed."
    echo "Investigat then remove $ISOIMAGE"
    exit $STATUS
fi

echo "ISO image built; burning to cd..."

SPD=8
OPTS="-eject -v fs=64M driveropts=burnproof"
cdrecord $OPTS -speed=$SPD dev=${CDDEV} $ISOIMAGE
STATUS=$?
if (( STATUS != 0 ))
then
    echo "Error. CD Burn failed."
    echo "Investigat then remove $ISOIMAGE"
    exit $STATUS
fi

rm -f $ISOIMAGE
echo "Done"
